package face.pay.web.oem.controller;

import face.pay.entity.UserInfo;
import face.pay.security.annotation.CurrentUser;
import face.pay.security.exception.BaseException;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import face.pay.web.oem.service.IUserManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/oem_user")
@Slf4j
public class UserManageController {

    @Autowired
    private IUserManageService IUserManageService;


    /**
     * 跳转用户列表页面
     *
     * @return
     */
    @RequestMapping("/list")
    public String manager() {
        return "oem/userManageList";
    }

    /**
     * 分页查询当前oem的用户
     * @param loginUser
     * @param pageParam
     * @return
     */
    @RequestMapping("/queryData")
    @ResponseBody
    public PageData data(@CurrentUser UserInfo loginUser, PageParam pageParam, UserInfo userInfo) {
        userInfo.setOemId(loginUser.getOemId());
        userInfo.setDeleted(0);
        return IUserManageService.queryPage(userInfo, pageParam);
    }

    /**
     * 代理商变更   查询oem下所有代理商
     *
     * @param userInfo
     * @param pageParam
     * @return
     */
    @RequestMapping("/agencyData")
    @ResponseBody
    public PageData agencyData(@CurrentUser UserInfo loginUser, PageParam pageParam, UserInfo userInfo){
        userInfo.setOemId(loginUser.getOemId());
        userInfo.setDeleted(0);
        userInfo.setType("AGENCY");
        return IUserManageService.queryPage(userInfo,pageParam);
    }

    /**
     * 修改用户删除标记  0：未删除 1：已删除
     * @param userInfo
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public ResultMessage updateUserInfo(UserInfo userInfo, @CurrentUser UserInfo loginUser){
        userInfo.setUpdatedBy(loginUser.getId());
        return IUserManageService.deleted(userInfo);
    }

    /**
     * 修改用户状态
     * @param userInfo
     * @param loginUser
     * @return
     */
    @RequestMapping("/lock")
    @ResponseBody
    public ResultMessage modifyUserState(UserInfo userInfo, @CurrentUser UserInfo loginUser){
        userInfo.setUpdatedBy(loginUser.getId());
        return IUserManageService.modifyUserState(userInfo);
    }

    /**
     * 修改代理商和商家推荐人
     * @param userInfo
     * @param loginUser
     * @return
     */
    @RequestMapping("/modifyUserRecommend")
    @ResponseBody
    public ResultMessage modifyUserRecommend(UserInfo userInfo, @CurrentUser UserInfo loginUser){
        userInfo.setUpdatedBy(loginUser.getId());
        userInfo.setAgencyId(userInfo.getRecommendId());
        if(userInfo.getId().equals(userInfo.getRecommendId())){
            String message = "推荐人不能为自己";
            return ResultMessage.createFail(message);
        }
        return IUserManageService.modifyUserRecommend(userInfo);
    }

    /**
     * 编辑用户基本信息
      * @param userInfo
     * @return
     */
    @RequestMapping("/editUserInfo")
    @ResponseBody
    public ResultMessage modifyUserInfo(UserInfo userInfo,@CurrentUser UserInfo loginUser){
        userInfo.setUpdatedBy(loginUser.getId());
        return IUserManageService.modifyUserInfo(userInfo);
    }


}
